package com.yfree.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yfree.R;
import com.yfree.YConfig;
import com.yfree.activities.YActivity;

public class YToastUtils {
    private static Toast toast;

    public static void toast(Context context, final CharSequence message) {
        if (toast != null) {
            try {
                toast.cancel();
            } catch (Exception ignored) {
            }
        }
        if (context instanceof YActivity) {
            YActivity activity = (YActivity) context;
            if (!activity.isAlive()) {
                return;
            }
        }
        toast = new Toast(context.getApplicationContext());
        View toastView = LayoutInflater.from(context).inflate(R.layout.toast_layout, null);
        ((ImageView) toastView.findViewById(R.id.imageView)).setImageResource(YConfig.logo_icon);
        toast.setView(toastView);
        toast.setDuration(Toast.LENGTH_LONG);
        ((TextView) toastView.findViewById(R.id.textView_toast)).setText(message);
        toast.show();
    }
}
